package features.advance.ch3.webserver.demo1;

import java.io.ByteArrayOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;
import java.net.InetAddress;
import java.net.Socket;

/**
 * @author LIN
 * @date 2022-09-18 21:45
 */
public class Client {

    public static void main(String[] args) throws IOException
    {
        System.out.println("------------------------------------");
        System.out.println("Client start......");
        System.out.println("------------------------------------");
        byte[] msg = new String("connect successfully!!!").getBytes();

        InetAddress inetAddr = InetAddress.getLocalHost();
        Socket client = new Socket(inetAddr, 8888);
        OutputStream out = client.getOutputStream();
        InputStream in = client.getInputStream();

        out.write(msg);
        out.flush();
        // 情况一 ：客户端在发送完最后一个字节后立即调用close( )方法
        client.close();

        ByteArrayOutputStream byteArrayOut = new ByteArrayOutputStream();
        byte[] temp = new byte[1024];
        int realLen = 0;
        while ((realLen = in.read(temp)) != -1)
        {
            byteArrayOut.write(temp, 0, realLen);
        }

        byte[] recv = byteArrayOut.toByteArray();

        System.out.println("Client receive msg:" + new String(recv));

        /*
         * 切记：在这里关闭输入流，并不会使服务端的输入流到达流末尾返回-1，仅仅是释放资源而已
         */
        in.close();
        out.close();

    }

}
